'use client'

import React, { useEffect, useState } from 'react';
import { 
  Button, 
  Modal, 
  ModalContent, 
  ModalHeader, 
  ModalBody, 
  ModalFooter,
  Input,
  Spinner
} from '@nextui-org/react';
import { BiTag } from 'react-icons/bi';
import ToastUtil from '@/app/utils/toastUtil';
import createAxiosClient from '@/app/utils/axiosRequest';

/**
 * 标签信息接口
 */
export interface TagItem {
  id: number;
  name: string;
  userId: number;
  createdAt: string;
  updatedAt: string;
}

/**
 * 标签列表响应
 */
export interface TagListResponse {
  total: number;
  size: number;
  current: number;
  records: TagItem[];
}

/**
 * Tag管理组件 - 提供标签相关API
 */
export function useTags() {
  /**
   * 获取用户标签列表
   */
  const getUserTags = async (current: number = 1, size: number = 20): Promise<TagListResponse | null> => {
    // 使用统一的axios实例
    const apiClient = createAxiosClient();
    
    try {
      const response = await apiClient.get(`/app/api/v1/tags`, {
        params: { current, size }
      });
      
      const data = response.data;
      if (data.status) {
        return data.data;
      } else {
        ToastUtil(data.message || "获取标签失败", "❌", "error");
        return null;
      }
    } catch (error: any) {
      ToastUtil(error.message || "获取标签异常", "❌", "error");
      return null;
    }
  };

  /**
   * 创建标签
   */
  const createTag = async (name: string): Promise<boolean> => {
    // 使用统一的axios实例
    const apiClient = createAxiosClient();
    
    try {
      const response = await apiClient.post(`/app/api/v1/tags`, null, {
        params: { name }
      });
      
      const data = response.data;
      if (data.status) {
        ToastUtil(data.message || "创建成功", "✅", "success");
        return true;
      } else {
        ToastUtil(data.message || "创建失败", "❌", "error");
        return false;
      }
    } catch (error: any) {
      ToastUtil(error.message || "创建标签异常", "❌", "error");
      return false;
    }
  };

  /**
   * 更新标签
   */
  const updateTag = async (tagId: number, name: string): Promise<boolean> => {
    // 使用统一的axios实例
    const apiClient = createAxiosClient();
    
    try {
      const response = await apiClient.put(`/app/api/v1/tags/${tagId}`, null, {
        params: { name }
      });
      
      const data = response.data;
      if (data.status) {
        ToastUtil(data.message || "更新成功", "✅", "success");
        return true;
      } else {
        ToastUtil(data.message || "更新失败", "❌", "error");
        return false;
      }
    } catch (error: any) {
      ToastUtil(error.message || "更新标签异常", "❌", "error");
      return false;
    }
  };

  /**
   * 删除标签
   */
  const deleteTag = async (tagId: number): Promise<boolean> => {
    // 使用统一的axios实例
    const apiClient = createAxiosClient();
    
    try {
      const response = await apiClient.delete(`/app/api/v1/tags/${tagId}`);
      
      const data = response.data;
      if (data.status) {
        ToastUtil(data.message || "删除成功", "✅", "success");
        return true;
      } else {
        ToastUtil(data.message || "删除失败", "❌", "error");
        return false;
      }
    } catch (error: any) {
      ToastUtil(error.message || "删除标签异常", "❌", "error");
      return false;
    }
  };

  return {
    getUserTags,
    createTag,
    updateTag,
    deleteTag
  };
}